package com.germinus.telcoblocks.echarts.projects;
import java.io.IOException;
import java.net.URI;
import java.util.Properties;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

import com.germinus.telcoblocks.echarts.natures.ProjectNature;
import com.germinus.telcoblocks.echarts.preferences.TelcoblocksPreferenceHandler;

/**
 * Class for supporting the Telcoblocks Eclipse project.
 * 
 * It creates the new project and calls to the "telcoblocks_appgen" python script in order
 * to create the folder structure.
 *  
 * @author Pablo Muñoz
 *
 */
public class TelcoBlocksProjectSupport {
	
    /**
     * We need to create a new default project and launch the telcoblocks_appgen script
     * 
     * @param projectName name for the new project
     * @param location location for the new project
     * @param props properties of the new project
     * @param monitor progress monitor
     * @return
     */
    public static IProject createProject(String projectName, URI location, Properties props,IProgressMonitor monitor) {
        Assert.isNotNull(projectName);
        Assert.isTrue(projectName.trim().length() > 0);
        IProject project = createBaseProject(projectName, location,monitor);
        if (props.size()!=4){
        	return null;
        }
        String applicationType = props.getProperty("ApplicationType");
        String packageName = props.getProperty("PackageName");
        String className = props.getProperty("ClassName");
        String appTitle = props.getProperty("AppTitle");        
        try {
            addNature(project);
            String EDK_HOME;
            String env = System.getenv("EDK_HOME");
            if (env!=null){
            	EDK_HOME = env;
            }
            else{
            	EDK_HOME = TelcoblocksPreferenceHandler.getEChartsPath();
            }
            String command = EDK_HOME + "/bin/telcoblocks_appgen " + project.getLocation() + " " + applicationType + " " + packageName + " " + className + " " + formatTitle(appTitle);
			Runtime runtime = Runtime.getRuntime();
			runtime.exec(command);        
        } catch (CoreException e1) {
            e1.printStackTrace();
            project = null;
        }
         catch (IOException e2){
        	 e2.printStackTrace();
        	 project = null;
         }
         catch(Exception e3){
        	 e3.printStackTrace();
        	 project = null;
         }
        return project;
    }

    /**
     * Creates a basic project.
     *
     * @param location 
     * @param projectName
     */
    private static IProject createBaseProject(String projectName, URI location,IProgressMonitor monitor) {
        IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        if (!newProject.exists()) {
            URI projectLocation = location;
            IProjectDescription desc = newProject.getWorkspace().newProjectDescription(newProject.getName());
            if (location != null && ResourcesPlugin.getWorkspace().getRoot().getLocationURI().equals(location)) {
                projectLocation = null;
            }

            desc.setLocationURI(projectLocation);
            try {
                newProject.create(desc, monitor);
                if (!newProject.isOpen()) {
                    newProject.open(monitor);
                }
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }

        return newProject;
    }


    /**
     * Add the nature to the project
     * 
     * @param project
     * @throws CoreException
     */
    private static void addNature(IProject project) throws CoreException {
        if (!project.hasNature(ProjectNature.NATURE_ID)) {
            IProjectDescription description = project.getDescription();
            String[] prevNatures = description.getNatureIds();
            String[] newNatures = new String[prevNatures.length + 1];
            System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
            newNatures[prevNatures.length] = ProjectNature.NATURE_ID;
            description.setNatureIds(newNatures);

            IProgressMonitor monitor = null;
            project.setDescription(description, monitor);
        }
    }
    
    
    private static String formatTitle(String text){
    			  return text.replace(' ' , '_');
    }
}
